package com.beiing.tianshuai.tianshuai.mine.model;

import com.beiing.tianshuai.tianshuai.mine.entity.MineOrderDataBean;
import com.beiing.tianshuai.tianshuai.mine.entity.OrderTransportDataBean;
import com.beiing.tianshuai.tianshuai.network.DataManager;
import com.beiing.tianshuai.tianshuai.util.LogUtils;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by Administrator on 2018/4/7.
 */

public class OrderTransportModel implements OrderTransportModelImpl{

    private OrderTransportModel.OrderTransportDataResponseListener mListener;
    private static final String TAG = "OrderTransportModel";
    public OrderTransportModel(OrderTransportModel.OrderTransportDataResponseListener listener) {
        mListener = listener;
    }
    public interface OrderTransportDataResponseListener{
        void onGetDetailSuccess(OrderTransportDataBean bean);

        void onGetDetailError(Throwable throwable);

    }
    @Override
    public void getOrderDetail(String oid) {
        RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                //如果请求字段错误，会因为返回的请求体格式改变而报错
                .addFormDataPart("order_id", oid)
                .build();
        Observable<OrderTransportDataBean> observable = DataManager.getOrderTransportData(body);
        Observable<OrderTransportDataBean> observeOn = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        observeOn.subscribe(new DisposableObserver<OrderTransportDataBean>() {
            @Override
            public void onNext(OrderTransportDataBean bean) {
                if (mListener != null) {
                    mListener.onGetDetailSuccess(bean);
                }
            }

            @Override
            public void onError(Throwable e) {
                if (mListener != null) {
                    LogUtils.e(e.getMessage(), TAG);
                    mListener.onGetDetailError(e);
                }
            }
            @Override
            public void onComplete() {

            }
        });
    }
}
